/**
 * Funcion que devuelve una imagen svg de un higchart
 * @author Mauricio Chamorro <mrchamorro@utn.edu.ec>
 * @param {type} charts
 * @returns {String}
 */
Highcharts.getSVG = function(charts) {
    var svgArr = [],
            top = 0,
            width = 0;

    $.each(charts, function(i, chart) {
        var svg = chart.getSVG();
        svg = svg.replace('<svg', '<g transform="translate(0,' + top + ')" ');
        svg = svg.replace('</svg>', '</g>');

        top += chart.chartHeight;
        width = Math.max(width, chart.chartWidth);

        svgArr.push(svg);
    });
    return '<svg height="' + top + '" width="' + width + '" version="1.1" xmlns="http://www.w3.org/2000/svg">' + svgArr.join('') + '</svg>';
};

/**
 * Funcion que permite exportar un array de charts de higchart
 * @author Mauricio Chamorro <mrchamorro@utn.edu.ec>
 * @param {type} charts
 * @param {type} options
 * @returns {undefined}
 */
Highcharts.exportCharts = function(charts, options) {
    var form
    svg = Highcharts.getSVG(charts);

    // merge the options
    options = Highcharts.merge(Highcharts.getOptions().exporting, options);

    // create the form
    form = Highcharts.createElement('form', {
        method: 'post',
        action: options.url
    }, {
        display: 'none'
    }, document.body);

    // add the values
    Highcharts.each(['filename', 'type', 'width', 'svg'], function(name) {
        Highcharts.createElement('input', {
            type: 'hidden',
            name: name,
            value: {
                filename: options.filename || 'chart',
                type: options.type,
                width: options.width,
                svg: svg
            }[name]
        }, null, form);
    });
    // submit
    form.submit();

    // clean up
    form.parentNode.removeChild(form);
};


/**
 * Permite imprimir multiples highcharts
 * @author Mauricio Chamorro <mrchamorro@utn.edu.ec>
 * @param {type} charts
 * @returns {undefined}
 */
function printCharts(charts) {
    var origDisplay = [],
            origParent = [],
            body = document.body,
            childNodes = body.childNodes;
    // hide all body content
    Highcharts.each(childNodes, function(node, i) {
        if (node.nodeType === 1) {
            origDisplay[i] = node.style.display;
            node.style.display = "none";
        }
    });
    // put the charts back in
    $.each(charts, function(i, chart) {
        origParent[i] = chart.container.parentNode;
        body.appendChild(chart.container);
    });
    // print
    window.print();
    // allow the browser to prepare before reverting
    setTimeout(function() {
        // put the chart back in
        $.each(charts, function(i, chart) {
            origParent[i].appendChild(chart.container);
        });

        // restore all body content
        Highcharts.each(childNodes, function(node, i) {
            if (node.nodeType === 1) {
                node.style.display = origDisplay[i];
            }
        });
    }, 500);
}